﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using TMDT.BusinessObjects;

namespace TMDT.BusinessLogic
{
    public class ChiTietThuocTinhManager
    {
        public bool Add(ChiTietThuocTinh entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spChiTietThuocTinhInsert",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDLoaiSanPham",
                                                                                             entity.IDLoaiSanPham),
                                                                            new SqlParameter("@IDThuocTinh",
                                                                                             entity.IDThuocTinh)
                                                                        }
                                                    });
        }

        public bool Update(ChiTietThuocTinh entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spChiTietThuocTinhUpdate",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDLoaiSanPham",
                                                                                             entity.IDLoaiSanPham),
                                                                            new SqlParameter("@IDThuocTinh",
                                                                                             entity.IDThuocTinh)
                                                                        }
                                                    });
        }


        public List<ChiTietThuocTinh> GetChiTietThuocTinh()
        {
            List<ChiTietThuocTinh> chiTietThuocTinh = new List<ChiTietThuocTinh>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from ChiTietThuocTinh",
                                                         CommandType = CommandType.Text,
                                                     });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                chiTietThuocTinh.Add(ChiTietThuocTinh.Convert(row));
            }

            return chiTietThuocTinh;
        }

        public bool IsChiTietThuocTinhTonTai(ChiTietThuocTinh chiTietThuocTinh)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
            {
                CommandText = string.Format("select * from ChiTietThuocTinh where IDLoaiSanPham = '{0}' and IDThuocTinh = '{1}'", chiTietThuocTinh.IDLoaiSanPham, chiTietThuocTinh.IDThuocTinh),
                CommandType = CommandType.Text,
            });
            if (dtSet.Tables[0].Rows.Count == 0)
                return false;

            return true;
        }

        public bool Remove(ChiTietThuocTinh entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
            {
                CommandText = "spChiTietThuocTinhDelete",
                CommandType = CommandType.StoredProcedure,
                Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDLoaiSanPham",entity.IDLoaiSanPham) ,
                                                                            new SqlParameter("@IDThuocTinh",entity.IDThuocTinh) 
                                                                        }
            });
        }

        public List<ChiTietThuocTinh> GetChiTietThuocTinhByIDLoai(object id)
        {
            List<ChiTietThuocTinh> chiTietThuocTinh = new List<ChiTietThuocTinh>();
            DataSet dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                             {
                                                                 CommandText = "select * from ChiTietThuocTinh where IDLoaiSanPham = @IDLoaiSanPham",
                                                                 CommandType = CommandType.StoredProcedure,
                                                                 Parameter = new[]
                                                                                 {
                                                                                     new SqlParameter("IDLoaiSanPham",id)
                                                                                 }
                                                             });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                chiTietThuocTinh.Add(ChiTietThuocTinh.Convert(row));
            }
            return chiTietThuocTinh;
        }
    }
}
